import java.nio.file.LinkOption;

public class MySingleList implements Ilist{

    public ListNode head;

    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
        }else{
            node.next = head;
            head = node;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
            return;
        }
        ListNode cur = head;
        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }

    private ListNode FindNode(int index){
        ListNode cur = head;
        while(index != 1){
            cur = cur.next;
        }
        return cur;
    }

    @Override
    public void addIndex(int index, int data) {
        int len = size();
        if(index < 0 || index > len){
            return;
        }
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == len){
            addLast(data);
            return;
        }
        ListNode cur = FindNode(index);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;

    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur.next != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if(head.val == key){
            head = head.next;
        }
        ListNode cur = head;
        while (cur.next != null){
            if(cur.next.val == key){
                cur.next = cur.next.next;
                return;
            }
            cur = cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur = head;
        while (cur != null){
            remove(key);
            cur = cur.next;
        }
    }

    @Override
    public int size() {
        ListNode cur = head;
        int count = 0;
        while (cur != null){
            cur = cur.next;
            count++;
        }
        return count;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while (cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while (cur != null){
            ListNode curN = cur.next;
            cur.next = null;
            cur = curN;
        }
        head = null;
    }

    public MySingleList.ListNode partition(MySingleList.ListNode head, int x) {
        MySingleList.ListNode bs = null;
        MySingleList.ListNode be = null;
        MySingleList.ListNode as = null;
        MySingleList.ListNode ae = null;
        MySingleList.ListNode cur = head;

        while (cur != null) {
            if (cur.val < x) {
                //第一次插入
                if (bs == null) {
                    bs = be = cur;
                } else {
                    be.next = cur;
                    be = be.next;
                }
            } else {
                if (as == null) {
                    as = ae = cur;
                } else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if (bs == null) {
            return as;
        }
        be.next = as;
        //不管第二部分是否最后一个节点为空 都手动置为空
        if (as != null) {
            ae.next = null;
        }
        return bs;
    }

    /*public ListNode ReverseSystem(ListNode head){

        ListNode cmp = head.next;
        ListNode tmp = cmp.next;
        head = null;

        while (tmp != null){
            cmp.next = head;
            head = cmp;
            cmp = tmp;
            tmp = tmp.next;
        }

        head.
    }*/

}
